import {
  checkReceivePayment,
  getReceivePaymentDetail,
  getReceivePaymentDetailOrderList,
  getReceivePaymentList,
  getReceivePaymentListQty,
  getUserUnpaidList,
  receivePaymentFormSave,
  writeBackNoSyncReceivePayment,
} from '@/api/withdrawal'
import { ref } from 'vue'

/**
 * 获取收款列表
 */
export const useGetReceivePaymentList = () => {
  // 获取收款列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref<WithdrawalAPIRequest.GetReceivePaymentListParam>({
    endTime: '',
    limit: 50,
    page: 1,
    receivePaymenNo: '',
    startTime: '',
    status: '',
    workNo: '',
    repairCompanyUser: '',
  })

  // 列表
  const list = ref<Array<WithdrawalReceivePayment>>([])

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getReceivePaymentList(getListParam.value)
    getListLoading.value = false
    if (res.data.code === 20000) {
      list.value = res.data.data.rows
      listTotal.value = Number(res.data.data.total)
      return ''
    }
    return res.data.message
  }

  /**
   * 重置搜索条件
   */
  const resetFilter = () => {
    getListParam.value = {
      endTime: '',
      limit: 50,
      page: 1,
      receivePaymenNo: '',
      startTime: '',
      status: null,
      workNo: '',
      repairCompanyUser: '',
    }
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
    resetFilter,
  }
}

/**
 * 获取客户未付款的工单列表
 */
export const useGetUserUnpaidList = () => {
  // 获取工单列表加载
  const getListLoading = ref(false)

  // 客户未付款的工单列表
  const list = ref<Array<WithdrawalUserUnpaid>>([])

  // 获取客户未付款的工单列表的请求参数
  const getListParam = ref({
    limit: 50,
    page: 1,
  })

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取列表
   * @param {string} userId - 客户id
   */
  const getList = async (userId: string) => {
    getListLoading.value = true
    const res = await getUserUnpaidList(userId, getListParam.value)
    getListLoading.value = false
    if (res.data.code === 20000) {
      listTotal.value = Number(res.data.data.total)
      list.value = res.data.data.rows
      return ''
    }
    return res.data.message
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 付款单表单
 */
export const useReceivePaymentForm = () => {
  // 表单
  const form = ref<WithdrawalAPIRequest.ReceivePaymentForm>({
    paymentOrderVos: [],
    remark: '',
    userId: '',
    userPhone: '',
    username: '',
  })

  // 表单保存加载
  const formSaveLoading = ref(false)

  /**
   * 表单保存
   */
  const formSave = async () => {
    formSaveLoading.value = true
    const res = await receivePaymentFormSave(form.value)
    formSaveLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      paymentOrderVos: [],
      remark: '',
      userId: '',
      userPhone: '',
      username: '',
    }
  }

  return {
    form,
    formSaveLoading,
    formSave,
    resetForm,
  }
}

/**
 * 获取收款列表每个状态对应的数量
 */
export const useGetReceivePaymentListQty = () => {
  // 获取数量加载
  const getListQtyLoading = ref(false)

  // 数量
  const listQty = ref<WithdrawalReceivePaymentListQty>({
    waitCheckCount: '',
    total: '',
    CheckedCount: '',
  })

  /**
   * 获取数量
   */
  const getListQty = async () => {
    getListQtyLoading.value = true
    const res = await getReceivePaymentListQty()
    getListQtyLoading.value = false
    if (res.data.code === 20000) {
      listQty.value = res.data.data.row
      return ''
    }
    return res.data.message
  }

  return {
    listQty,
    getListQtyLoading,
    getListQty,
  }
}

/**
 * 审核收款单
 */
export const useCheckReceivePayment = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkForm = ref<WithdrawalAPIRequest.ReceivePaymentCheck>({
    checkResultType: 1,
    receivePaymentIds: [],
    remark: '',
  })

  /**
   * 审核
   */
  const check = async () => {
    checkLoading.value = true
    const res = await checkReceivePayment(checkForm.value)
    checkLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    checkForm.value = {
      checkResultType: 1,
      receivePaymentIds: [],
      remark: '',
    }
  }

  return {
    checkForm,
    checkLoading,
    check,
    resetForm,
  }
}

/**
 * 获取收款单详情
 */
export const useGetReceivePaymentDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 详情
  const detail = ref<WithdrawalReceivePaymentDetail>({
    checkEmp: '',
    checkRemark: '',
    checkTime: '',
    createEmp: '',
    receivePaymentCreateTime: '',
    receivePaymentId: '',
    receivePaymentNo: '',
    remark: '',
    userId: '',
    userPhone: '',
    username: '',
    repairCompanyUser: '',
    repairCompanyUserId: '',
    repairCompanyUserPhone: '',
    sendForRepairPhone: '',
    sendForRepairUserId: '',
    sendForRepairUsername: '',
  })

  /**
   * 获取详情
   * @param {string} id - 收款单id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getReceivePaymentDetail(id)
    getDetailLoading.value = false
    if (res.data.code === 20000) {
      detail.value = res.data.data.row
      return ''
    }
    return res.data.message
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 获取收款单详情的工单列表
 */
export const useGetReceivePaymentDetailOrderList = () => {
  // 获取工单列表加载
  const getOrderListLoading = ref(false)

  // 工单列表
  const orderList = ref<Array<WithdrawalReceivePaymentDetailOrder>>([])

  /**
   * 获取工单列表
   * @param {string} id - 收款单id
   */
  const getOrderList = async (id: string) => {
    getOrderListLoading.value = true
    const res = await getReceivePaymentDetailOrderList(id)
    getOrderListLoading.value = false
    if (res.data.code === 20000) {
      orderList.value = res.data.data.rows
      return ''
    }

    return res.data.message
  }

  return {
    orderList,
    getOrderListLoading,
    getOrderList,
  }
}

/**
 * 反写为同步的云星空的应收款单
 */
export const useWriteBackNoSyncReceivePayment = () => {
  // 反写加载
  const writeBackLoading = ref(false)

  /**
   * 反写
   * @param {string} id - 收款单id
   */
  const write = async (id: string) => {
    writeBackLoading.value = true
    const res = await writeBackNoSyncReceivePayment(id)
    writeBackLoading.value = false
    return res.data
  }

  return {
    writeBackLoading,
    write,
  }
}
